var onlylyric = {
    onLoad: function() {
        // initialization code
        this.initialized = true;
        this.strings = document.getElementById("onlylyric-strings");

        try{
            const cid = "@zlbruce.blogspot.com/onlylyric;1";
            this.xpcom_OnlyLyric = Components.classes[cid].createInstance(Components.interfaces.IOnlyLyric);

            this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
            this.prefs = this.prefs.getBranch("extensions.OnlyLyric.");

            var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
            observerService.addObserver(this.xpcom_OnlyLyric,"quit-application",false);

            onlylyric.SetPrefsToXpcom();
        }
        catch(err)
        {
            alert(err);
        }
        setInterval("onlylyric.getLrc();", 200);
    },
    getLrc: function() {
        var res = this.xpcom_OnlyLyric.GetString();
        var panel = document.getElementById("onlylyric-display");
        panel.setAttribute("value", res);
    },

    onSetLrcFile: function() {
        var nsIFilePicker = Components.interfaces.nsIFilePicker;
        var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
        var lrcfilepath = this.prefs.getCharPref("lrcfilepath");

        if(lrcfilepath.length != 0)
        {
            var path = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            path.initWithPath(lrcfilepath);
            fp.displayDirectory = path;
        }
        fp.init(window, "Select a File", nsIFilePicker.modeOpen);
        fp.appendFilter("lrc files(*.lrc | *.txt)", "*.lrc; *.txt");
        fp.appendFilters(nsIFilePicker.filterAll);

        var res = fp.show();
        if(res == nsIFilePicker.returnOK)
        {
            this.prefs.setCharPref("lrcfilepath", fp.file.parent.path);
            this.xpcom_OnlyLyric.SetLrcFile(fp.file.path);
        }
    },

    SetPlayer: function(player) {
        if(player == 'winamp')
        {
            this.xpcom_OnlyLyric.SetPlayer("winamp");
            this.prefs.setCharPref("player", "winamp");
        }
        else if(player == 'itunes')
        {
            this.xpcom_OnlyLyric.SetPlayer("itunes");
            this.prefs.setCharPref("player", "itunes");
        }
        else if(player == 'wmp')
        {
            this.xpcom_OnlyLyric.SetPlayer("wmp");
            this.prefs.setCharPref("player", "wmp");
        }
	else if(player == 'xmms')
        {
            this.xpcom_OnlyLyric.SetPlayer("xmms");
            this.prefs.setCharPref("player", "xmms");
        }
    },
 
    onSetLrcDir: function() {
        var nsIFilePicker = Components.interfaces.nsIFilePicker;
        var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
        var lrcfiledir = this.prefs.getCharPref("lrcfiledir");

        if(lrcfiledir.length != 0)
        {
            var path = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            path.initWithPath(lrcfiledir);
            fp.displayDirectory = path;
        }
        fp.init(window, "Select the lrc's folder", nsIFilePicker.modeGetFolder);

        var res = fp.show();
        if(res == nsIFilePicker.returnOK)
        {
            this.prefs.setCharPref("lrcfiledir", fp.file.path);
            this.xpcom_OnlyLyric.SetLrcDir(fp.file.path);
        }
    },


    SetPrefsToXpcom: function() {
        // set lrc map file
        //var file = Components.classes["@mozilla.org/file/directory_service;1"]
        //.getService( Components.interfaces.nsIProperties)
        //.get("ProfD", Components.interfaces.nsIFile);
        //file.append("OnlyLyric-MapLrc.map");
        //this.xpcom_OnlyLyric.SetMapFile(file.path);

        var lrcfiledir = this.prefs.getCharPref("lrcfiledir");
        var player = this.prefs.getCharPref("player");
        var lrcencode = this.prefs.getCharPref("lrcencode");
        var local = this.prefs.getCharPref("local");

        if(lrcfiledir.length != 0)
        {
            this.xpcom_OnlyLyric.SetLrcDir(lrcfiledir);
        }
        if(player.length != 0)
        {
            this.xpcom_OnlyLyric.SetPlayer(player);
        }
        if(lrcencode.length != 0)
        {
            this.xpcom_OnlyLyric.SetLrcEncode(lrcencode);
        }
        if(local.length != 0)
        {
            this.xpcom_OnlyLyric.SetLocal(local);
        }
    },

    SetFileEncode: function(code) {
        this.prefs.setCharPref("lrcencode", code);
        this.xpcom_OnlyLyric.SetLrcEncode(code);

    },

    SetLocal: function(code) {
        this.prefs.setCharPref("local", code);
        this.xpcom_OnlyLyric.SetLocal(code);
    },

};
window.addEventListener("load", function(e) { onlylyric.onLoad(e); }, false);
